//
//  LCInfoCoheardCell.m
//  TheLCNews
//
//  Created by apple on 15/12/1.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCInfoCoheardCell.h"
#import "LCInformationModel.h"
#import "LCCarouselTbCell.h"
#import "LCInfoTbCell.h"
#import "LCjockTbCell.h"
#import "LCInfoWebVC.h"
@implementation LCInfoCoheardCell


#pragma mark 初始化方法

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self news];
         self.webDataArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
-(void)news{
    
    self.newsTableView = [[UITableView alloc] init];
    
    self.newsTableView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.newsTableView];
    self.newsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createData];
    }];
    self.newsTableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [self fotter];
    }];
    
    self.newsTableView.delegate = self;
    self.newsTableView.dataSource = self;
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    self.newsTableView.frame = CGRectMake(0, 0, CWIDTH, CHEIGHT);
    
}

- (void)setTitle:(NSString *)title{
    if (_title != title) {
        _title = title;
        [self createData];
    }
}



#pragma mark - 数据解析
- (void)createData{
    self.hud = [[MBProgressHUD alloc] initWithView:self.newsTableView];
    [self.contentView addSubview:_hud];
    [self.contentView bringSubviewToFront:_hud];
    //self.hud.delegate = self;
    self.hud.labelText = @"加载中...";
    [self.hud show:YES];

    // 此步得到数据解析中需要的ID
    NSString *path = [[NSBundle mainBundle] pathForResource:@"GPSTitle" ofType:@"plist"];
    
    
    NSMutableArray *pathArr = [NSMutableArray arrayWithContentsOfFile:path];
    NSString *titleId = nil;
    for (NSInteger i = 0; i < pathArr.count; i++) {
        if ([self.title isEqualToString:pathArr[i][@"title"] ]) {
            titleId = pathArr[i][@"id"];
            break;
        }
    }
    
    
    [LYLAFNetWorking GETURLByString:[NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=%@,SYRECOMMEND&page=1&gv=4.4.7&av=4.4.7&uid=867588027089230&deviceid=867588027089230&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=720x1280&publishid=6001", titleId] AFData:^(id result) {
        NSArray *arr = result;
        self.webDataArr = [NSMutableArray array];
        for (NSDictionary *tempDic in arr[0][@"item"]) {
            LCInformationModel *model = [[LCInformationModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [model setValue:tempDic[@"style"][@"images"] forKey:@"images"];
            [self.webDataArr addObject:model];
            
        }
        
        if (arr[1] != nil) {
            self.scrollViewDataArr = [LCScrollModel baseModelByArr:arr[1][@"item"]];
        }
        
        if (self.webDataArr.count > 0) {
            [self.newsTableView reloadData];
        }
        [self.newsTableView.mj_header endRefreshing];
        [self.hud removeFromSuperview];
        [self createMainScrollView];
    }];
    
    
}

#pragma mark 轮播图创建
- (void)createMainScrollView{
    self.mainScrollView = [[LCMainScrollView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT / 3.70555556) index:self.scrollViewDataArr.count modelArray:self.scrollViewDataArr];
    self.newsTableView.tableHeaderView = _mainScrollView;
   
    
    self.newsTableView.showsVerticalScrollIndicator = NO;
    
    // 添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    [_mainScrollView addGestureRecognizer:tap];
}

//  手势方法
- (void)click:(UITapGestureRecognizer *)tap{
    for (NSInteger i = 0; i < self.scrollViewDataArr.count; i++) {
        if (self.mainScrollView.contentOffset.x / CWIDTH == i) {
            LCScrollModel *model = self.scrollViewDataArr[i];
            [self.scrollDelegate skipWebView:model];
        }
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.webDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCInformationModel * model = self.webDataArr[indexPath.row];
    
    
    
    
    if ([model.type isEqualToString:@"doc"]) {
        static NSString *typeOneReuse = @"typeOneReuse";
        LCCarouselTbCell *cell = [tableView dequeueReusableCellWithIdentifier:typeOneReuse];
        if (!cell) {
            cell = [[LCCarouselTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:typeOneReuse];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = model.title;
        [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.timeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentlabel.text = [NSString stringWithFormat:@"评论:%@", model.commentsall];
        
        // 字体适配
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            cell.titleLabel.font = [UIFont systemFontOfSize:12.];
            cell.timeLabel.font = [UIFont systemFontOfSize:9.];
            cell.commentlabel.font = [UIFont systemFontOfSize:9.];
        }
        else if ([UIScreen mainScreen].bounds.size.height == 568){
            cell.titleLabel.font = [UIFont systemFontOfSize:14.];
            cell.timeLabel.font = [UIFont systemFontOfSize:11.];
            cell.commentlabel.font = [UIFont systemFontOfSize:11.];
        }
        else{
            cell.titleLabel.font = [UIFont systemFontOfSize:17.];
            cell.timeLabel.font = [UIFont systemFontOfSize:14.];
            cell.commentlabel.font = [UIFont systemFontOfSize:14.];
        }
        
        return cell;
    }
    else if (model.thumbnail.length == 0 && [model.type isEqualToString:@"doc"]){
        static NSString *typeThreeReuse = @"typeThreeReuse";
        LCjockTbCell *cell = [tableView dequeueReusableCellWithIdentifier:typeThreeReuse];
        if (!cell) {
            cell = [[LCjockTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:typeThreeReuse] ;
        }
        cell.titleLabel.text = model.title;
        cell.updateTimeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentsallLabel.text = [NSString stringWithFormat:@"评论:%@", model.commentsall];
        
               // 字体适配
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            cell.titleLabel.font = [UIFont systemFontOfSize:12.];
        }
        else if ([UIScreen mainScreen].bounds.size.height == 568){
            cell.titleLabel.font = [UIFont systemFontOfSize:14.];
            
        }
        else{
            cell.titleLabel.font = [UIFont systemFontOfSize:17.];
        }
        
        
        return cell;
    }
    else if([model.type isEqualToString:@"slide"]){
        static NSString *typeTwoReuse = @"typeTwoReuse";
        LCInfoTbCell *cell = [tableView dequeueReusableCellWithIdentifier:typeTwoReuse];
        if (!cell) {
            cell = [[LCInfoTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:typeTwoReuse] ;
        }
        cell.selectionStyle = 0;
        cell.titleLabel.text = model.title;
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:model.images[0]] placeholderImage:[UIImage imageNamed:@""]];
        [cell.centerImage sd_setImageWithURL:[NSURL URLWithString:model.images[1]] placeholderImage:[UIImage imageNamed:@""]];
        [cell.rightImage sd_setImageWithURL:[NSURL URLWithString:model.images[2]] placeholderImage:[UIImage imageNamed:@""]];
        cell.updateTimeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentLabel.text = [NSString stringWithFormat:@"评论:%@", model.commentsall];
        
        
        // 字体适配
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            cell.titleLabel.font = [UIFont systemFontOfSize:12.];
        }
        else if ([UIScreen mainScreen].bounds.size.height == 568){
            cell.titleLabel.font = [UIFont systemFontOfSize:14.];
            
        }
        else{
            cell.titleLabel.font = [UIFont systemFontOfSize:17.];
        }
        
        
        return cell;
    }
    else{
        static NSString *noReuse = @"noReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:noReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:noReuse];
        }
        
        return cell;
    }
    return nil;
    
    
}

#pragma mark tableView显示行高方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCInformationModel *model = self.webDataArr[indexPath.row];
    if ([model.type isEqualToString:@"doc"]) {
        return KHEIGHT / 5.6;
    }
    else if(model.thumbnail.length == 0 && [model.type isEqualToString:@"doc"]){
        return KHEIGHT / 7.411111111111;
    }
    else if([model.type isEqualToString:@"slide"]){
        return KHEIGHT / 4.45;
    }
    else{
        return 0;
    }
}



#pragma mark tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LCInformationModel *model= self.webDataArr[indexPath.row];
    [self.delegate messageWebModel:model];
}


- (void)fotter{
    self.page ++;
    // 此步得到数据解析中需要的ID
    NSString *path = [[NSBundle mainBundle] pathForResource:@"GPSTitle" ofType:@"plist"];
    
    
    NSMutableArray *pathArr = [NSMutableArray arrayWithContentsOfFile:path];
    NSString *titleId = nil;
    for (NSInteger i = 0; i < pathArr.count; i++) {
        if ([self.title isEqualToString:pathArr[i][@"title"] ]) {
            titleId = pathArr[i][@"id"];
            break;
        }
    }
    
    
    [LYLAFNetWorking GETURLByString:[NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=%@,SYRECOMMEND&page=%ld&gv=4.4.7&av=4.4.7&uid=867588027089230&deviceid=867588027089230&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=720x1280&publishid=6001", titleId,self.page] AFData:^(id result) {
        NSArray *arr = result;
       
        for (NSDictionary *tempDic in arr[0][@"item"]) {
            LCInformationModel *model = [[LCInformationModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [model setValue:tempDic[@"style"][@"images"] forKey:@"images"];
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObject:model];
            [self.webDataArr addObjectsFromArray:arr];
            
        }
        
        //if (arr[1] != nil) {
           // self.scrollViewDataArr = [LCScrollModel baseModelByArr:arr[1][@"item"]];
       // }
        
        if (self.webDataArr.count > 0) {
            [self.newsTableView reloadData];
        }
        
       // [self createMainScrollView];
    }];

}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
